package com.nsqt.common.service.impl;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import com.nsqt.common.dao.GeneratorMapper;
import com.nsqt.common.service.GeneratorService;
import com.nsqt.common.utils.GenUtils;
import com.nsqt.common.utils.WordUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

@Service
public class GeneratorServiceImpl implements GeneratorService {
    @Autowired
    GeneratorMapper generatorMapper;

    @Override
    public List<Map<String, Object>> list() {
        List<Map<String, Object>> list = generatorMapper.list();
        return list;
    }

    @Override
    public byte[] generatorCode(String[] tableNames) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = generatorMapper.get(tableName);
            //查询列信息
            List<Map<String, String>> columns = generatorMapper.listColumns(tableName);
            //生成代码
            GenUtils.generatorCode(table, columns, zip);


        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    @Override
    public void generateDocument(String[] tableNames) {
        WordUtils wordUtils = new WordUtils();
        try {
            wordUtils.openDocument("C:\\Users\\admin\\Downloads\\XXXX系统使用说明书 V1.0.doc");
            wordUtils.insertTitle("XXXX系统使用说明书 V1.0", 18, Font.BOLD, Element.ALIGN_CENTER);
            wordUtils.intertTitlePattern("安装配置", RtfParagraphStyle.STYLE_HEADING_1);
            wordUtils.insertTitlePatternSecond("环境配置", RtfParagraphStyle.STYLE_HEADING_2);
            String context = "一、支持系统windows7/windows8/windows10及windows server 2008/2012/2016。\n";
            context += "二、安装jdk，并配置系统环境变量JAVA_HOME，指向jdk安装目录。\n";
            context += "三、安装mysql数据库，版本5.6或以上。\n";
            context += "四、安装tomcat8.5中间件。\n";
            context += "\n";
            wordUtils.insertContext(context, 12, com.lowagie.text.Font.NORMAL, Element.ALIGN_LEFT);
            wordUtils.insertTitlePatternSecond("系统安装", RtfParagraphStyle.STYLE_HEADING_2);
            context = "一、拷贝fullmedia.war到C:\\tomcat8.5\\webapps目录下。";
            context += "二、打开命令行窗口，切换到C:\\tomcat8.5\\bin目录下，执行startup.bat。\n";
            context += "\n";
            wordUtils.insertContext(context, 12, com.lowagie.text.Font.NORMAL, Element.ALIGN_LEFT);

            wordUtils.insertTitlePatternSecond("系统登录", RtfParagraphStyle.STYLE_HEADING_2);
            context = "一、打开浏览器，在地址栏输入http://ipaddress:9092/login，进入系统登录页面。\n";
            context += "二、在系统欢迎页面，输入用户名，密码，点击【登录】按钮，登录系统。\n";
            context += "\n";
            wordUtils.insertContext(context, 12, com.lowagie.text.Font.NORMAL, Element.ALIGN_LEFT);

            for (String tableName : tableNames) {
                //查询表信息
                Map<String, String> table = generatorMapper.get(tableName);
                //查询列信息
                List<Map<String, String>> columns = generatorMapper.listColumns(tableName);
                //生成代码
                wordUtils.generateDocument(table, columns);
            }
            wordUtils.generateSystem();
            wordUtils.closeDocument();

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
